<?php

function HLStoRGB($H,$L,$S) {
	$p1 = 1;
	$p2 = 1;

	$rgb = array(3);
	if ($L <= 0.5){
		$p2 = $L * (1 + $S);
	}else{
		$p2 = $L + $S - ($L * $S);
	}
	$p1 = 2*$L - $p2;
	if ($S==0) {
		$rgb[0] = $L;
		$rgb[1] = $L;
		$rgb[2] = $L;
	} else {
		$rgb[0] = RGB($p1, $p2, $H + 120);
		$rgb[1] = RGB($p1, $p2, $H);
		$rgb[2] = RGB($p1, $p2, $H - 120);
	}
	$rgb[0] *= 255;
	$rgb[1] *= 255;
	$rgb[2] *= 255;
	return $rgb;
}

function RGB($q1,$q2,$hue) {
	if ($hue > 360){
		$hue = $hue - 360;
	}
	if ($hue < 0){
		$hue = $hue + 360;
	}
	if ($hue < 60){
		return ($q1 + ($q2 - $q1) * $hue / 60);
	}else if ($hue < 180){
		return($q2);
	}else if ($hue < 240){
		return ($q1 + ($q2 - $q1) * (240 - $hue)/60);
	}else{
		return ($q1);
	}
}

$globalColorH = 120 ;
$globalColorD = 30 ;
$globalColorS = 0.5 ;
$colorAlpha = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F') ;
$globalColorHKey = "button_globalColorHKey";
$globalColorDKey = "button_globalColorDKey";
$globalColorSKey = "button_globalColorSKey";

if(get($globalColorHKey) != ""){
	$_SESSION[$globalColorHKey] = get($globalColorHKey);
}
if(isset($_SESSION[$globalColorHKey])){
	$globalColorH = intval($_SESSION[$globalColorHKey]) ;
}

if(get($globalColorDKey) != ""){
	$_SESSION[$globalColorDKey] = get($globalColorDKey);
}
if(isset($_SESSION[$globalColorDKey])){
	$globalColorD = intval($_SESSION[$globalColorDKey]) ;
}

if(get($globalColorSKey) != ""){
	$_SESSION[$globalColorSKey] = get($globalColorSKey);
}
if(isset($_SESSION[$globalColorSKey])){
	$globalColorS = floatval($_SESSION[$globalColorSKey]) ;
}

// echo $globalColorH."/".$globalColorD."/".$globalColorS."<br><br>";

function getRGBColor($degree){
	global $globalColorH , $globalColorS ;
	$degree = $degree / 100 ;
	$rgb = HLStoRGB($globalColorH, $degree, $globalColorS);
	return "#" . toHexString($rgb[0]) . toHexString($rgb[1]) . toHexString($rgb[2]) ;
}

function getColor($h, $d ,$s){
	$d = $d / 100 ;
	$rgb = HLStoRGB($h, $d, $s);
	return "#" . toHexString($rgb[0]) . toHexString($rgb[1]) . toHexString($rgb[2]) ;
}

function getCurrentColor(){
	global $globalColorH,$globalColorD,$globalColorS;
	$rgb = HLStoRGB($globalColorH, $globalColorD / 100, $globalColorS);
	return "#" . toHexString($rgb[0]) . toHexString($rgb[1]) . toHexString($rgb[2]) ;
}

function getGrayColor($degree){
	$degree = $degree / 100 ;
	$rgb = HLStoRGB(0, $degree, 0);
	return "#" . toHexString($rgb[0]) . toHexString($rgb[1]) . toHexString($rgb[2]) ;
}

function toHexString($n){
	global $colorAlpha ;
	$n = round($n);
	$a = $n % 16 ;
	$b = (($n - $a) / 16) % 16 ;
	// print("[" . $n . "/" . $b . "/" . $a . "]");
	return $colorAlpha[$b] . $colorAlpha[$a] ;
}

function getColorChooseTable(){
	global $globalColorH,$globalColorD,$globalColorS;
	$html = "";
	// $html .= "<div style='width:100%;'>";
	$html .= "<TABLE align=''>";
	$html .= "<TR>";

	for($i=0;$i<10;$i++){
		$dd = ($i+1) * 30 ;
		$html .= "<TD style='border:black 1px solid;background-color:".getColor($dd,50,1).";width:8px;height:8px;cursor:pointer;' onclick=\"changeGlobalColor('".$dd."')\"></TD>";
		$html .= "<TD style='border:black 0px solid;width:5px;'></TD>";
	}

	$html .= "</TR>";
	$html .= "</TABLE>";
	// $html .= "</div>";

	return $html ;
}

function getLightChooseTable(){
	global $globalColorH,$globalColorD,$globalColorS;
	$html = "";
	$html .= "<div style='width:400px;'>";
	$html .= "<TABLE>";
	$html .= "<TR>";

	for($i=0;$i<10;$i++){
		$dd = ($i + 1) * 10 ;
		$html .= "<TD style='border:black 1px solid;background-color:".getColor($globalColorH,$dd,1).";width:8px;height:8px;cursor:pointer;' onclick=\"changeGlobalDegree('".$dd."')\"></TD>";
		$html .= "<TD style='border:black 0px solid;width:5px;'></TD>";
	}

	$html .= "</TR>";
	$html .= "</TABLE>";
	$html .= "</div>";

	return $html ;
}

function getDegreeChooseTable(){
	global $globalColorH,$globalColorD,$globalColorS;

	$html = "";
	$html .= "<div style='width:200px;'>";
	$html .= "<TABLE>";
	$html .= "<TR>";

	for($i=0;$i<10;$i++){
		$dd = ($i + 1) * 0.02 ;
		$html .= "<TD style='border:black 1px solid;background-color:".getColor($globalColorH, 50 ,$dd).";width:8px;height:8px;cursor:pointer;' onclick=\"changeGlobalLight('".$dd."')\"></TD>";
		$html .= "<TD style='border:black 0px solid;width:5px;'></TD>";
	}

	$html .= "</TR>";
	$html .= "</TABLE>";
	$html .= "</div>";

	return $html ;
}

function getFullColorTable(){
	$html = "";
	$html .= "<div style=''>";
	$html .= "<TABLE>";
	
	for($i=0;$i<10;$i++){
		$html .= "<TR>";
		for($j=4;$j<5;$j++){
			for($t=-1;$t<10;$t++){

				$h = ($i+1) * 30 ;
				$d = ($j+1) * 10 ;
				$s = ($t+1) * 0.1;

				$html .= "<TD style='border:black 1px solid;background-color:".getColor($h,$d,$s).";width:18px;height:18px;cursor:pointer;' onclick=\"changeColor(".$h.",".$d.",".$s.")\"></TD>";
			//	$html .= "<TD style='border:black 0px solid;width:5px;'></TD>";
			}
		}
	
		$html .= "</TR>";
	}
	
	$html .= "</TABLE>";
	$html .= "</div>";

	return $html ;
}

function printColorSettingsToJS(){
	global $globalColorH , $globalColorS ;
	global $colorAlpha ;

	$CRLF = "\r\n";
	$html = "";
	$html .= $CRLF."var globalColorH = " . $globalColorH . ";";
	$html .= $CRLF."var globalColorS = " . $globalColorS . ";";
	$html .= $CRLF;
	$html .= $CRLF;

	return $html ;
}

// printColorSettingsToJS();

$color1 = getRGBColor(10);
$color2 = getRGBColor(20);
$color3 = getRGBColor(30);
$color4 = getRGBColor(40);
$color5 = getRGBColor(50);
$color6 = getRGBColor(60);
$color7 = getRGBColor(70);
$color8 = getRGBColor(80);
$color9 = getRGBColor(90);
$color96 = getRGBColor(96);

?>